
import os

from initialize import init1, init2
from tests import test1, test2, test3
from cleanup import cleanUp1, cleanUp2

from xithyl import logger
from xithyl.utils import fork

error=""

logger.addLogger(__name__)

def Start(res=True):

    global error
    if not res:
        logger.log("Setting Fail Message", __name__)
        error = "Predefined Fail Message"
        logger.log("Setting Fail Message done", __name__)
        return False

    def firstCase():
        logger.log("Initialize 1", __name__)
        objects = init1()
        logger.log("Initialize 1 Done", __name__)

        logger.log("Test 1", __name__)
        test1(objects)
        logger.log("Test 1 Done", __name__)

        logger.log("Clean Up 1", __name__)
        cleanUp1(objects)
        logger.log("Clean Up 1 Done", __name__)
        return True

    def secondCase():
        logger.log("Initialize 2", __name__)
        objects = init2()
        logger.log("Initialize 2 Done", __name__)

        logger.log("Test 2", __name__)
        test2(objects)
        logger.log("Test 2 Done", __name__)

        logger.log("Clean Up 2", __name__)
        cleanUp2(objects)
        logger.log("Clean Up 2 Done", __name__)
        return True

    def thirdCase():
        logger.log("Initialize 3", __name__)
        objects = init1()
        logger.log("Initialize 3 Done", __name__)

        logger.log("Test 3", __name__)
        test3(objects)
        logger.log("Test 3 Done", __name__)

        logger.log("Clean Up 3", __name__)
        cleanUp1(objects)
        logger.log("Clean Up 3 Done", __name__)
        return False


    ret = fork.runSafe(firstCase, True)
    if ret != os.EX_OK:
        return False

    ret = fork.runSafe(secondCase, True)
    if ret != os.EX_OK:
        return False

    ret = fork.runSafe(thirdCase, True)
    # 11 == segfault
    if ret != 11:
        return False

    return True

def ErrorMsg():
    global error
    return error

